package booksys.book;



import booksys.common.Utils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BookService {

    public Map<String, Object> getBookList(QueryDto queryDto) {
        SqlSession sess = Utils.openSession();
        try {
            BookDao dao = sess.getMapper(BookDao.class);

            //执行映射器方法之前执行分页参数(页码，每页记录数)设置
            PageHelper.startPage(queryDto);


            List<Book> bookList = dao.findBookList(queryDto);


            //执行映射器方法之后，立即执行PageInfo对象创建
            PageInfo<Book> pageInfo = new PageInfo<Book>(bookList);

            Map<String,Object> page = new HashMap<>();
            page.put("current",pageInfo.getPageNum());//当前页
            page.put("pageSize",pageInfo.getPageSize());//每页最大记录数
            page.put("total",pageInfo.getTotal());//总记录数
            page.put("pages",pageInfo.getPages());//总页数
            page.put("size",pageInfo.getSize());//当前页记录数
            page.put("list",pageInfo.getList());//当前页数据记录
            page.put("first",1);
            page.put("pre",pageInfo.getPrePage());
            page.put("next",pageInfo.getNextPage());
            page.put("last",pageInfo.getPages());


            sess.commit();

            return page;

        } catch (Exception e) {
            sess.rollback();
            System.out.println(e);
            throw new RuntimeException("加载图书信息失败！", e);
        } finally {
            sess.close();
        }
    }

    public void addBook(BookDto dto) {

        SqlSession sess = Utils.openSession();
        try {
            BookDao dao = sess.getMapper(BookDao.class);

            if (StringUtil.isEmpty(dto.getB_id())){
                throw new RuntimeException("图书编号错误！图书编号不能为空");
            }

            boolean hasstudent = dao.findBookbyId(dto.getB_id());
            if (hasstudent) {
                throw new RuntimeException("图书编号错误！已存在编号");
            }


            dao.insertBook(dto);

            sess.commit();

        } catch (Exception e) {
            sess.rollback();
            if (e instanceof RuntimeException) {
                throw e;
            }
            throw new RuntimeException("新增图书失败！", e);


        } finally {
            sess.close();
        }
    }
//
    public void updBook(BookDto dto) {
        SqlSession sess = Utils.openSession();
        try {
            BookDao dao = sess.getMapper(BookDao.class);
            
                dao.updateBook(dto);
                
            sess.commit();

        } catch (Exception e) {
            sess.rollback();
            if (e instanceof RuntimeException) {
                throw e;
            }
            throw new RuntimeException("修改图书信息失败！", e);
        } finally {
            sess.close();
        }
    }
//
    public void delBook(BookDto dto) {
        SqlSession sess = Utils.openSession();
        try {
            BookDao dao = sess.getMapper(BookDao.class);

            if(dao.getbb_retime(dto.getB_id())){
                throw new RuntimeException("图书未归还，禁止删除");
            }

            dao.deleteBook(dto.getB_id());
            sess.commit();
        } catch (Exception e) {
            sess.rollback();
            if (e instanceof RuntimeException) {
                throw e;
            }
            throw new RuntimeException("删除图书失败!", e);
        } finally {
            sess.close();
        }
    }
}
